import request from './request.js';
import { message } from '../utils/index.js';
import router from '../router/index.js';

export const userinfo = () => request({
    url: '/admin/user/userinfo',
    method: 'GET'
});

export const login = (username, password) => {
    return request.post('/admin/user/login', { username, password })
    .then(resp => {
        router.push('/');
        message.success('登录成功');
        if (resp.code === 200) 
            request.setAuthorization(resp.data);
        return resp;
    });
};

export const logout = () => {
    return request.delete('/admin/user/logout')
    .then(resp => {
        if ( resp.code === 200 ) 
            request.setAuthorization(null);
        return resp;
    }).then(resp => {
        router.push('/login');
        message.success('注销成功');
        return resp;
    });
};

export default {
    login,
    logout,
    userinfo,
};